# Be sure to go and modify make.paths if you want to compile CNC or SWARM!
# By default only sequential and omp are enabled
include make.paths

ARCH := intel64


SOURCES_C = $(wildcard *.c)
SOURCES_CPP = $(wildcard *.cpp)
DEST_OBJS_C :=$(SOURCES_C:.c=.o)
DEST_OBJS_CPP :=$(SOURCES_CPP:.cpp=.opp)
TARGETS := stencil


export OPT_LEVEL := -O3
ifeq ($(DEBUG), 1)
export OPT_LEVEL := -O0 -g -DDEBUG  -Wall -W -pedantic
endif

export OPT = -fast
export CXX = g++
export OPT := $(OPT_LEVEL)
export OPTCXX := $(OPT_LEVEL) -std=c++11 
export CC=gcc
export CLANG_LIBCPP_INCS=

ifeq ($(CLANG),1)
export CXX = clang++
export OPTCXX := -std=c++11 $(OPT_LEVEL)
export OPT := $(OPT_LEVEL)
export CC = clang
export CLANG_LIBCPP_INCS = -I/usr/gcc-4.7.2/include/c++/4.7.2/x86_64-linux-gnu -I/usr/gcc-4.7.2/include/c++/4.7.2/
endif

ifeq ($(ICC),1)
export CXX = icpc
export OPTCXX := $(OPT_LEVEL) -fp-model precise -vec_report=3
export CC = icc
export OPT := $(OPT_LEVEL) -fp-model precise
export CLANG_LIBCPP_INCS=
endif

OPENMP ?= 1
ifeq ($(OPENMP),1)
export OPENMP_OPT = -fopenmp
ifeq ($(ICC),1)
export OPENMP_OPT = -openmp
endif
ifeq ($(CLANG),1)
export OPENMP_OPT =
endif
export OPT += $(OPENMP_OPT)
export OPTCXX += $(OPENMP_OPT)
endif


# Options to be set at compile time
ifdef VERBOSE
export OPTIONS += -DVERBOSE=$(VERBOSE)
endif

ifdef DEBUG_ET_DEPS
export OPTIONS += -DDEBUG_ET_DEPS=1
endif

ifdef MEASURE_OVERHEADS
export OPTIONS += -DMEASURE_OVERHEADS=$(MEASURE_OVERHEADS)
endif

ifdef ISOLATE_SEQ_OVERHEADS
export OPTIONS += -DISOLATE_SEQ_OVERHEADS=$(ISOLATE_SEQ_OVERHEADS)
endif

ifndef NO_STATIC_ALLOC
export OPTIONS += -DSTATIC_ALLOC
endif


all: 
	@echo $(COMMENTS)
	$(MAKE_CNC)
	$(MAKE_SWARM)
	make -C base
	make -C support
	make stencil

test_build:
	$(MAKE_CNC)
	$(MAKE_SWARM)
	make -C base
	make -C support
	make stencil

test_build_options:
	sh ./testBuilds.sh 

test:
	make clean -C simple_expression_template
	make -C simple_expression_template 2> err
	@echo "Number of test errors in expression templates : " $(shell grep error err | wc -l)
	@rm err

%.opp: %.cpp
	$(CXX) -c $(OPTCXX) $(OPTIONS) $(CLANG_LIBCPP_INCS) $(OS_INCLUDE) $(INCLUDES) -o $@ $< 

%.o: %.c
	$(CC) -c  $(OPT) $(OPTIONS) $(INCLUDES) -o $@ $< 

stencil: $(DEST_OBJS_CPP) $(DEST_OBJS_C) */*.o */*.opp
	$(CXX) -o $@ $(DEST_OBJS_C) $(DEST_OBJS_CPP) */*.o */*.opp -std=c++11 $(LIBS) -lpthread -lrt -fopenmp

clean:
	make clean -C base
	make clean -C support
	make clean -C swarm
	make clean -C cnc
	make clean -C simple_expression_template
	rm -f $(TARGETS) $(DEST_OBJS_C) $(DEST_OBJS_CPP) *~ a.out dmp err */*~
